package com.yunke.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.YunkeBanner;
import com.yunke.entity.YunkeHomeModule;
import com.yunke.service.YunkeBannerService;
import com.yunke.service.YunkeHomeModuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@RestController
@RequestMapping("/home")
@Tag(name = "首页管理", description = "首页相关接口")
public class YunkeHomeController {

    @Autowired
    private YunkeHomeModuleService homeModuleService;

    @Autowired
    private YunkeBannerService bannerService;

    @GetMapping("/modules")
    @Operation(summary = "查询首页展示模块")
    public YunkeResult<List<YunkeHomeModule>> getHomeModules(
            @Parameter(description = "目标用户类型") @RequestParam(required = false) Integer targetUser) {
        
        List<YunkeHomeModule> modules = homeModuleService.getHomeModules(targetUser);
        return YunkeResult.success(modules);
    }

    @GetMapping("/modules/position/{position}")
    @Operation(summary = "根据位置查询模块列表")
    public YunkeResult<List<YunkeHomeModule>> getModulesByPosition(
            @Parameter(description = "显示位置") @PathVariable String position,
            @Parameter(description = "目标用户类型") @RequestParam(required = false) Integer targetUser) {
        
        List<YunkeHomeModule> modules = homeModuleService.getModulesByPosition(position, targetUser, true);
        return YunkeResult.success(modules);
    }

    @GetMapping("/modules/type/{type}")
    @Operation(summary = "根据类型查询模块列表")
    public YunkeResult<List<YunkeHomeModule>> getModulesByType(
            @Parameter(description = "模块类型") @PathVariable Integer type) {
        
        List<YunkeHomeModule> modules = homeModuleService.getModulesByType(type, true);
        return YunkeResult.success(modules);
    }

    @PostMapping("/modules/{id}/click")
    @Operation(summary = "更新模块点击统计")
    public YunkeResult<Boolean> updateModuleClick(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.updateClickCount(id);
        return YunkeResult.success(result);
    }

    @PostMapping("/modules/{id}/view")
    @Operation(summary = "更新模块浏览统计")
    public YunkeResult<Boolean> updateModuleView(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.updateViewCount(id);
        return YunkeResult.success(result);
    }

    @GetMapping("/banners")
    @Operation(summary = "查询首页轮播图")
    public YunkeResult<List<YunkeBanner>> getHomeBanners(
            @Parameter(description = "目标用户类型") @RequestParam(required = false) Integer targetUser) {
        
        List<YunkeBanner> banners = bannerService.getHomeBanners(targetUser);
        return YunkeResult.success(banners);
    }

    @GetMapping("/banners/position/{position}")
    @Operation(summary = "根据位置查询轮播图列表")
    public YunkeResult<List<YunkeBanner>> getBannersByPosition(
            @Parameter(description = "显示位置") @PathVariable String position,
            @Parameter(description = "目标用户类型") @RequestParam(required = false) Integer targetUser) {
        
        List<YunkeBanner> banners = bannerService.getBannersByPosition(position, targetUser, true);
        return YunkeResult.success(banners);
    }

    @GetMapping("/banners/type/{type}")
    @Operation(summary = "根据类型查询轮播图列表")
    public YunkeResult<List<YunkeBanner>> getBannersByType(
            @Parameter(description = "轮播图类型") @PathVariable Integer type) {
        
        List<YunkeBanner> banners = bannerService.getBannersByType(type, true);
        return YunkeResult.success(banners);
    }

    @PostMapping("/banners/{id}/click")
    @Operation(summary = "更新轮播图点击统计")
    public YunkeResult<Boolean> updateBannerClick(
            @Parameter(description = "轮播图ID") @PathVariable Long id) {
        
        boolean result = bannerService.updateClickCount(id);
        return YunkeResult.success(result);
    }

    @PostMapping("/banners/{id}/view")
    @Operation(summary = "更新轮播图浏览统计")
    public YunkeResult<Boolean> updateBannerView(
            @Parameter(description = "轮播图ID") @PathVariable Long id) {
        
        boolean result = bannerService.updateViewCount(id);
        return YunkeResult.success(result);
    }

    @GetMapping("/modules/count")
    @Operation(summary = "统计模块数量")
    @SaCheckLogin
    public YunkeResult<Long> countModules(
            @Parameter(description = "模块类型") @RequestParam(required = false) Integer moduleType,
            @Parameter(description = "显示位置") @RequestParam(required = false) String displayPosition) {
        
        long count = homeModuleService.countModules(moduleType, displayPosition, true);
        return YunkeResult.success(count);
    }

    @GetMapping("/banners/count")
    @Operation(summary = "统计轮播图数量")
    @SaCheckLogin
    public YunkeResult<Long> countBanners(
            @Parameter(description = "轮播图类型") @RequestParam(required = false) Integer bannerType,
            @Parameter(description = "显示位置") @RequestParam(required = false) String displayPosition) {
        
        long count = bannerService.countBanners(bannerType, displayPosition, true);
        return YunkeResult.success(count);
    }
} 